package org.pearl.mybatis.demo.test;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.pearl.mybatis.demo.dao.MenuMapper;
import org.pearl.mybatis.demo.dao.UserMapper;
import org.pearl.mybatis.demo.pojo.entity.User;
import org.pearl.mybatis.demo.pojo.qurey.UserQuery;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by TD on 2021/6/15
 */
public class CacheTest006 {
    public static void main(String[] args) throws IOException, InterruptedException {
/*        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 两个session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);



        // 第一次会话
        UserQuery userQuery = new UserQuery();
        userQuery.setLoginName("zhangwei");
        List<User> dynamicUserList = userMapper.selectDynamicUserList(userQuery);
        sqlSession.close();
        System.out.println(dynamicUserList);
        // 第二个会话再次查询
        List<User> dynamicUserList2 = userMapper2.selectDynamicUserList(userQuery);
        sqlSession2.close();
        System.out.println(dynamicUserList2);

        // 查询ehcache
        CacheManager cacheManager = CacheManager.create();
        String[] cacheNames = cacheManager.getCacheNames();
        for (String cacheName : cacheNames) {
            Cache cache = cacheManager.getCache(cacheName);
            List keys = cache.getKeys();
            for (Object key : keys) {
                System.out.println("查询ehcache"+ cache.get(key).toString());
            }
        }

    }*/
    }
}
